<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Service\PlayletService;
use App\Service\UserService;
use App\Service\CommService;
use App\Service\TaskService;

#[AutoController]
class TaskController extends BaseController
{
    #[Inject]
    private PlayletService $playletService;
    #[Inject]
    private UserService $userService;
    #[Inject]
    private CommService $commService;
    #[Inject]
    private TaskService $taskService;

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return $response->raw('Hello!');
    }

    public function sign_list(RequestInterface $request, ResponseInterface $response){
        # 签到列表
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list = $this->taskService->sign_list($applet_id,$user_id);
        $dates = date("Ymd");
        $data = [
            "is_sign_today"=>$this->taskService->get_day_sing_status($applet_id,$user_id,$dates),
            "list"=>$list,
        ];
        return $this->resultResponse(["data"=>$data]);
    }

    public function sign(RequestInterface $request, ResponseInterface $response){
        # 签到
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list = $this->taskService->sign($applet_id,$user_id);
        return $this->resultResponse(["data"=>$list]);
    }

    public function list(RequestInterface $request, ResponseInterface $response){
        # 任务列表
        echo "任务列表";
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        print_r($applet_id);
        print_r($xc_open_id);
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list = $this->taskService->task_list($applet_id,$user_id);
        return $this->resultResponse(["data"=>$list]);
    }

    public function do_task(RequestInterface $request, ResponseInterface $response){
        # 做任务
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        $stype = $request->input('stype', '1');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $list = $this->taskService->do_task($applet_id,$user_id,$stype);
        return $this->resultResponse(["data"=>$list]);
    }

    public function collect(RequestInterface $request, ResponseInterface $response){
        # 领取任务
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        $task_id = $request->input('task_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($task_id)){
            return $this->resultResponse(["code"=>20040]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $code = $this->taskService->collect($applet_id,$user_id,$task_id);
        return $this->resultResponse(["code"=>$code]);
    }

    public function complete(RequestInterface $request, ResponseInterface $response){
        echo "完成任务".PHP_EOL;
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input('xc_open_id', '');
        $task_id = $request->input('task_id', '');
        if (empty($xc_open_id) or empty($applet_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($task_id)){
            return $this->resultResponse(["code"=>20040]);
        }
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = $open_id_info['id'];
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        $code = $this->taskService->complete($applet_id,$user_id,$task_id);
        return $this->resultResponse(["code"=>$code]);
    }

}
